package com.devunion.bogdan.conf;

import java.io.*;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

/**
 * @author Viktar Liaskovich
 */
public class Config {
    private static Logger log = Logger.getLogger(Config.class);

    public static final String DEFAULT_ENCODING = "iso-8859-1";
    public static final String DEFAULT_HOST = "jdbc:mysql://localhost/<db_name>";
    public static final int DEFAULT_PORT = 3306;

    public static final File DEFAULT_PROJECT_HOME = new File(System.getProperty("user.dir"), ".DefaultEditor");
    public static final File IDE_HOME = new File(System.getProperty("user.home"), ".LibrarianEditor");

    public static final String PROPERTY_HOST = "ServerHost";
    public static final String PROPERTY_PORT = "ServerPort";

    public static final String PROPERTY_LOGIN = "UserLogin";
    public static final String PROPERTY_PASSWORD = "UserPassword";

    public static final String CONFIG_FILENAME = "config.ini";

    private static Properties properties = null;

    private String encoding = DEFAULT_ENCODING;

    static {
        DEFAULT_PROJECT_HOME.mkdirs();
        IDE_HOME.mkdirs();


        File configFile = new File(".", CONFIG_FILENAME);
//        File configFile = new File(IDE_HOME, CONFIG_FILENAME);

        if (!configFile.exists()) {
            properties = new Properties();
            saveProperties();
        }
    }

    public static Object getProperty(String key) {
        if (properties == null) {
            loadProperties();
        }
        return properties.get(key);
    }

    public static Integer getNumberProperty(String key) {
        if (properties == null) {
            loadProperties();
        }
        return (Integer) properties.get(key);
    }

    public static void setProperty(String key, String value) {
        properties.put(key, value);

        saveProperties();
    }

    private static void loadProperties() {
        File fileProperties = new File(CONFIG_FILENAME);
        properties = new Properties();

        if (fileProperties.exists()) {
            FileInputStream inStream = null;
            try {
                inStream = new FileInputStream(fileProperties);
                properties.load(inStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(inStream);
            }
        }
    }

    private static void saveProperties() {
        try {
            properties.store(new FileOutputStream(CONFIG_FILENAME), "User proprties");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Config() {
    }

    public String getEncoding() {
        return encoding;
    }

    /////////////////////////////////////////////

    // Getters and setters for all properties
    ///////////////////////////////////////////
    public static String getHost() {
        Object property = getProperty(PROPERTY_HOST);
        return property == null ? DEFAULT_HOST : (String) property;
    }

    public static void setHost(String host) {
        setProperty(PROPERTY_HOST, host);
    }

    public static Integer getPort() {
        Object port = getNumberProperty(PROPERTY_PORT);

        return port == null ? 3306 : Integer.valueOf(port.toString());
    }

    public static void setPort(int port) {
        setProperty(PROPERTY_PORT, String.valueOf(port));
    }

    public static String getLogin() {
        Object property = getProperty(PROPERTY_LOGIN);
        return property == null ? "" : (String) property;
    }

    public static void setLogin(String host) {
        setProperty(PROPERTY_LOGIN, host);
    }

    public static String getPassword() {
        Object property = getProperty(PROPERTY_PASSWORD);
        return property == null ? "" : (String) property;
    }

    public static void setPassowrd(String host) {
        setProperty(PROPERTY_PASSWORD, host);
    }


}
